﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//----------------------------------------
using DotNetNuke.Entities.Modules;
using DotNetNuke.UI.Utilities;
using System.Collections.Generic;

namespace VluSDCEvent
{
    public partial class CtrEventList : PortalModuleBase
    {
        #region Members *****************************************
        VluSDCEvent.Controller oCtrler;
        #endregion Members --------------------------------------

        #region Programmer Methods *****************************************
        List<TblSDCEvent> MtdGetEventList()
        {
            List<TblSDCEvent> lstEvent = new List<TblSDCEvent>();
            // Lấy phần tử con cuối cùng.
            TblSDCEvent oEvent =
                (from Eves in oCtrler.OLinqData.TblSDCEvents
                 where Eves.IsDelete == false
                 && Eves.ChildId == null
                 && Eves.ModuleDefID == oCtrler.MtdGetModuleDefId((this.Parent.Parent as PortalModuleBase).ModuleId, this.TabId)
                 select Eves).FirstOrDefault();
            if (oEvent != null)
            {
                if (MtdCheckDate(oEvent))
                    lstEvent.Add(oEvent);
                // Lặp lại cho mỗi cha.
                TblSDCEvent oEventParent =
                    (from Eves in oCtrler.OLinqData.TblSDCEvents
                     where Eves.IsDelete == false
                     && Eves.ChildId == oEvent.Id
                     && Eves.ModuleDefID == oCtrler.MtdGetModuleDefId((this.Parent.Parent as PortalModuleBase).ModuleId, this.TabId)
                     select Eves).FirstOrDefault();
                while (oEventParent != null)
                {
                    if (MtdCheckDate(oEventParent))
                        lstEvent.Add(oEventParent);
                    oEventParent =
                        (from Eves in oCtrler.OLinqData.TblSDCEvents
                         where Eves.IsDelete == false
                         && Eves.ChildId == oEventParent.Id
                         && Eves.ModuleDefID == oCtrler.MtdGetModuleDefId((this.Parent.Parent as PortalModuleBase).ModuleId, this.TabId)
                         select Eves).FirstOrDefault();
                }
            }
            else
                return null;
            return lstEvent;
        }

        bool MtdCheckDate(TblSDCEvent pEvent)
        {
             //DateTime dt = (new DotNetNuke.Entities.Users.UserTime()).CurrentUserTime.Date;
            DateTime dt = oCtrler.MtdGetDate().Date;
            return (pEvent.StartDate.Value.Date <= dt.Date && dt.Date <= pEvent.EndDate.Value.Date);
        }
        #endregion Programmer Methods --------------------------------------

        #region Dotnet Methods *****************************************
        protected void Page_Init(object sender, EventArgs e)
        {
            oCtrler = new VluSDCEvent.Controller();

            string jQueryPath = "~/Resources/Shared/scripts/jquery/jquery.min.js";

            ScriptManager sm = ScriptManager.GetCurrent(this.Page);
            string path1 = ResolveUrl(jQueryPath);
            //string path1 = this.TemplateSourceDirectory + "/js/jquery-1.3.js";
            //string path2 = this.TemplateSourceDirectory + "/js/jquery.marquee.js";
            string path3 = this.TemplateSourceDirectory + "/js/VNTMarquee.js";

            sm.Scripts.Add(new ScriptReference(path1));
            //sm.Scripts.Add(new ScriptReference(path2));
            sm.Scripts.Add(new ScriptReference(path3));
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "jQueryNoConflict", "jQuery.noConflict();", true);


            //string appPath = HttpContext.Current.Request.ApplicationPath;
            //if (!Page.ClientScript.IsClientScriptIncludeRegistered("VNTMarquee"))
            //{
            //    Page.ClientScript.RegisterClientScriptInclude("VNTMarquee", appPath + "/js/VNTMarquee.js");
            //}
            //if (!Page.ClientScript.IsClientScriptIncludeRegistered("VNTJQuery"))
            //{
            //    Page.ClientScript.RegisterClientScriptInclude("VNTJQuery", appPath + "/js/jquery-1.3.js");
            //}
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            dtlEvent.DataSource = MtdGetEventList();
            dtlEvent.DataBind();

            dtlEvent1.DataSource = dtlEvent.DataSource;
            dtlEvent1.DataBind();
         
            ModuleController objModuleController = new ModuleController();
            try
            {
                object radBulletImage = objModuleController.GetTabModuleSettings((this.Parent.Parent as PortalModuleBase).TabModuleId)["chkBulletImage"];
                if(radBulletImage == null)
                    objModuleController.UpdateTabModuleSetting((this.Parent.Parent as PortalModuleBase).TabModuleId, "chkBulletImage", "True");
                else
                {
                    foreach (DataListItem itm in dtlEvent.Items)
                    {
                        itm.FindControl("TableCell1").Visible = bool.Parse(radBulletImage.ToString());
                    }
                    foreach (DataListItem itm in dtlEvent1.Items)
                    {
                        itm.FindControl("TableCell1").Visible = bool.Parse(radBulletImage.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                VluSDCEvent.Controller.MtdLogException(ex);
            };

        }
        #endregion Dotnet Methods --------------------------------------
    }
}